#include "CueGLPicker.h"
#include <iostream>
namespace coolpool
{

	CueGLPicker::CueGLPicker(GLuint cue_name)
	{
		cue_name_ = cue_name;
		cue_was_picked_ = false;
	}

	CueGLPicker::~CueGLPicker()
	{

	}

	void CueGLPicker::processHits(GLint hits, GLuint buffer[])
	{
		GLuint names, *ptr, minZ, *ptrNames, numberOfNames;

		ptr = (GLuint *) buffer;
		minZ = 0xffffffff;
		for (int i = 0; i < hits; i++)
		{
			names = *ptr;
			ptr++;
			if (*ptr < minZ)
			{
				numberOfNames = names;
				minZ = *ptr;
				ptrNames = ptr + 2;
			}

			ptr += names + 2;
		}
		ptr = ptrNames;
		cue_was_picked_ = false;
		for (unsigned j = 0; j < numberOfNames; j++, ptr++)
		{
			if (*ptr == cue_name_)
			{
				cue_was_picked_ = true;
			}
		}
	}
}
